$(function() {
    $("#qualificationTable").bootstrapTable('destroy').bootstrapTable({
        url : window.portAddress + "/qualification/qualificationList",
        dataType : "json",
        method : 'get',
        striped: true,  //行间隔色
        cache : false,  //禁用缓存
        pageNumber: 1,  //初始化加载第一页
        pagination: true,//是否分页
        sidePagination: "client",//前端分页
        pageSize: 5,//单页记录数
        pageList: [5, 10, 15],//可选择单页记录数
        clickToSelect : true,
        theadClasses: "thead-pink",
        queryParams: function (params) {
            //向服务器传递的参数
            return {
               /* option : $("#search_option option:selected").val(),*/
                expirationDate : $("#expirationDate").val(),
                customerNo : $("#customerNo").val(),
                assetLevel : $("#assetLevel").val(),
                creditRating : $("#creditRating").val(),

            };
        },
        responseHandler : function(res) {
            return res.data;
        },
        columns : [
            {
                title : "资质到期日",
                field : "expirationDate",
                align : "center",

            },
            {
                title : "客户号",
                field : "customerNo",
                align : "center",
            },
            {
                title : "客户名",
                field : "customerName",
                align : "center",
            },
            {
                title : "专业投资机构",
                field : "professionalInvestor",
                align : "center",
            },
            {
                title : "期权交易",
                field : "allowedOption",
                align : "center",
            },
            {
                title : "预定汇率交易",
                field : "allowedBookRate",
                align : "center",
            },
            {
                title : "征信等级",
                field : "creditRating",
                align : "center",
            },
            {
                title : "资产等级",
                field : "assetLevel",
                align : "center",
            },
            {
                title : "操作",
                field : "pendingNumber",
                align : "center",
                formatter : operation,
                width : 130,
                height: 40,
            },

        ]
    });

    $("#search").click(function() {
        $.ajax({
            url : window.portAddress + "/qualification/qualificationList",
            type : "post",
            data : {
                expirationDate : $("#expirationDate").val(),
                customerNo : $("#customerNo").val(),
                assetLevel : $("#assetLevel").val(),
                creditRating : $("#creditRating").val(),
            },
            dataType : "json",
            success : function(data) {
                if (data.state === '000000') {
                    toastr.success("查询数据成功");
                }else {
                    toastr.error(data.message);
                }
                $("#qualificationTable").bootstrapTable("refresh");
            }
        })
    });

    $("#downTemplate").click(function() {

        var baseUrl = window.portAddress + "/qualification/downTemplate";
        window.location= baseUrl;

    });

    $("#upload").click(function() {
        var file = $("#qualificationFile")[0].files[0];
        if(file.name != "客户资质模板.xls"){
            toastr.warning("文件名或者格式不对!");
            $("#qualificationFile").val('');
            return
        }
        var formData = new FormData();
        formData.append("qualificationFile",file)
        formData.append("userName",window.sessionStorage.getItem("userName"))

        $.ajax({
            url : window.portAddress + "/qualification/upload",
            type : "post",
            data : formData,
            processData : false,
            contentType : false,
            success : function(data) {
                if (data.state === '000000') {
                    toastr.success("上传数据成功");
                    $("#qualificationFile").val('');
                }else {
                    toastr.error(data.message);
                }
                $("#qualificationTable").bootstrapTable("refresh");
            }
        })

    });

    $("#deleteModel").on("show.bs.modal",function (event){
        var thisBtn = $(event.relatedTarget);
        var customerNumber = thisBtn.closest('tr').find('td').eq(1).text();
        $("#deleteCustomerNumber").val(customerNumber);

        $.ajax({
            url : window.portAddress + "/qualification/deleteQualification",
            type : "post",
            data : {
                customerNo : $("#deleteCustomerNumber").val(),
                userName: window.sessionStorage.getItem("userName")
            },
            dataType : "json",
            success : function(data) {
                $("#qualificationTable").bootstrapTable("refresh");
                $("#deleteModel").modal('hide');

                if (data.state === '000000') {
                    toastr.success(data.message);
                }else {
                    toastr.error(data.message);
                }
            }
        })
    });

    $("#updateModel").on("show.bs.modal",function (event){
        var thisBtn = $(event.relatedTarget);
        var updateExpirationDate = thisBtn.closest('tr').find('td').eq(0).text();
        var customerNumber = thisBtn.closest('tr').find('td').eq(1).text();
        var customerName = thisBtn.closest('tr').find('td').eq(2).text();
        var updateProfessionalInvestor = thisBtn.closest('tr').find('td').eq(3).text();
        var updateAllowedOption = thisBtn.closest('tr').find('td').eq(4).text();
        var updateAllowedBookRate = thisBtn.closest('tr').find('td').eq(5).text();
        var updateCreditRating = thisBtn.closest('tr').find('td').eq(6).text();
        var updateAssetLevel = thisBtn.closest('tr').find('td').eq(7).text();

        $("#updateExpirationDate").val(updateExpirationDate);
        $("#customerNumber").val(customerNumber);
        $("#customerName").val(customerName);
        $("#updateProfessionalInvestor").val(updateProfessionalInvestor);
        $("#updateAllowedOption").val(updateAllowedOption);
        $("#updateAllowedBookRate").val(updateAllowedBookRate);
        $("#updateCreditRating").val(updateCreditRating);
        $("#updateAssetLevel").val(updateAssetLevel);
    });

    $("#updateBtn").click(function() {
        // 创建一个新的Date对象，它将包含当前日期和时间
        var date = new Date();

        var year = date.getFullYear();

        var month = date.getMonth() + 1;

        var day = date.getDate();

        // 可以按照需要格式化日期和时间
        var CurrentDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);

        if($("#updateExpirationDate").val() < CurrentDate){
            toastr.warning("输入日期不符合要求!");
            return;
        }

        if(!$("#updateAssetLevel").val().startsWith("A") || $("#updateAssetLevel").val().length !=3  || $("#updateAssetLevel").val().slice(-2) > 12 || $("#updateAssetLevel").val().slice(-2) <= 4){
            toastr.warning("资产等级输入不符合要求!");
            return;
        }

        $.ajax({
            url : window.portAddress + "/qualification/modifyQualification",
            type : "post",
            data : {
                expirationDate : $("#updateExpirationDate").val(),
                customerNo : $("#customerNumber").val(),
                customerName : $("#customerName").val(),
                professionalInvestor : $("#updateProfessionalInvestor").val(),
                allowedOption : $("#updateAllowedOption").val(),
                allowedBookRate : $("#updateAllowedBookRate").val(),
                creditRating : $("#updateCreditRating").val(),
                assetLevel : $("#updateAssetLevel").val(),
                userName: window.sessionStorage.getItem("userName")
            },
            dataType : "json",
            success : function(data) {
                $("#qualificationTable").bootstrapTable("refresh");
                $("#updateModel").modal('hide');
                if (data.state === '000000') {
                    toastr.success(data.message);
                }else {
                    toastr.error(data.message);
                }
            }
        })
    });


    $("#downPdf").click(function() {
        $.ajax({
            url : window.portAddress + "/qualification/downQualification",
            type : "post",
            data : {
                expirationDate : $("#expirationDate").val(),
                customerNo : $("#customerNo").val(),
                assetLevel : $("#assetLevel").val(),
                creditRating : $("#creditRating").val(),
                userName: window.sessionStorage.getItem("userName")
            },
            dataType : "json",
            success : function(data) {
                $("#qualificationTable").bootstrapTable("refresh");
                if (data.state === '000000') {
                    toastr.success(data.message);
                }else {
                    toastr.error(data.message);
                }
            }
        })


    });

    function operation(value,row,index) {
        var htm;
        if(value != null){
            return  htm = "<span class=\"badge badge-warning\"><font size='3'>审核中...</font></span>"
        }else{
            htm = "<button type=\"button\" class=\"btn btn-primary\" style='width: 40px;' data-target=\"#updateModel\" data-toggle=\"modal\"><span class=\"glyphicon glyphicon-pencil\"></span></button> "
                + "<button type=\"button\" class=\"btn btn-danger\" style='width: 40px;' data-target=\"#deleteModel\" data-toggle=\"modal\"><span class=\"glyphicon glyphicon-trash\"></span></button> ";
            return htm;
        }
    }


})
